﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace NPSoft.Ferrero.Framework.Utility
{
    /// <summary>
	/// Json帮助类
	/// </summary>
    public static class JsonHelper
    {

        private static JsonSerializerSettings _jsonSettings;

        static JsonHelper()
        {
            //IsoDateTimeConverter datetimeConverter = new IsoDateTimeConverter();
            //datetimeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            //_jsonSettings.Converters.Add(datetimeConverter);
            _jsonSettings = new JsonSerializerSettings();
            _jsonSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
            _jsonSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            _jsonSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        }

        /// <summary>
        /// 将对象序列化为Json字符串。
        /// </summary>
        /// <param name="value">要序列化的对象</param>
        /// <returns>Json字符串</returns>
        public static string SerializeObject(object value)
        {
            return JsonConvert.SerializeObject(value, Formatting.None, _jsonSettings);
        }

        /// <summary>
        /// 将对象序列化为Json字符串。
        /// </summary>
        /// <param name="values">要序列化的对象集合</param>
        /// <param name="properties">要序列化的属性集合</param>
        /// <param name="include">指定属性集合的属性是要包含的属性还是要排除的属性</param>
        /// <returns>Json字符串</returns>
        public static string SerializeObject<T>(List<T> values, List<string> properties, bool include)
        {
            if (values == null || values.Count == 0)
            {
                return SerializeObject(values);
            }
            if (properties == null || properties.Count == 0)
            {
                return SerializeObject(values);
            }
            IEnumerable<string> iprops;
            if (include)
            {
                iprops = (IEnumerable<string>)properties;
            }
            else
            {
                List<string> props = ReflectionHelper.GetObjectPropertyList(values[0]);
                iprops = props.Except(properties);
            }
            JArray data = new JArray();
            foreach (T item in values)
            {
                JObject jo = new JObject();
                foreach (string prop in iprops)
                {
                    jo.Add(new JProperty(prop, ReflectionHelper.GetObjectPropertyValue(item, prop)));
                }
                data.Add(jo);
            }
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 将Json字符串反序列化为指定类型的对象。
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="value">Json字符串</param>
        /// <returns>反序列化的对象</returns>
        public static T DeserializeObject<T>(string value)
        {
            return JsonConvert.DeserializeObject<T>(value, _jsonSettings);
        }
    }
}
